"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadSchema = exports.downloadSchemas = exports.writeSchemaToDisk = exports.getResolvedPath = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const prettier_1 = __importDefault(require("prettier"));
const path_2 = require("../lib/path");
const customObjects_1 = require("../api/customObjects");
function getResolvedPath(dest, name) {
    if (name)
        return path_1.default.resolve((0, path_2.getCwd)(), dest || '', `${name}.json`);
    return path_1.default.resolve((0, path_2.getCwd)(), dest || '');
}
exports.getResolvedPath = getResolvedPath;
async function writeSchemaToDisk(schema, dest) {
    const formattedSchema = await prettier_1.default.format(JSON.stringify(schema), {
        parser: 'json',
    });
    fs_extra_1.default.outputFileSync(getResolvedPath(dest, schema.name), formattedSchema);
}
exports.writeSchemaToDisk = writeSchemaToDisk;
async function downloadSchemas(accountId, dest) {
    const axiosResponse = await (0, customObjects_1.fetchObjectSchemas)(accountId);
    const response = axiosResponse.data;
    if (response.results.length) {
        for (const schema of response.results) {
            await writeSchemaToDisk(schema, dest);
        }
    }
    return response.results;
}
exports.downloadSchemas = downloadSchemas;
async function downloadSchema(accountId, schemaObjectType, dest) {
    const axiosResponse = await (0, customObjects_1.fetchObjectSchema)(accountId, schemaObjectType);
    const response = axiosResponse.data;
    await writeSchemaToDisk(response, dest);
    return response;
}
exports.downloadSchema = downloadSchema;
